from utils.HTTPRequests import HTTPRequests
import json
import configparser

# 创建一个ConfigParser对象
config = configparser.ConfigParser()

# 读取ini文件内容
config.read("config.ini")

# 获取ini文件中的数据
section = "config"
option = "apiKey"
API_KEY = config.get(section, option)


class AIService:
    BASE_URL = "https://dashscope.aliyuncs.com/api/v1"

    def imageSynthesis(desc):
        headers = {
            "X-DashScope-Async": "enable",
            "Authorization": "Bearer " + API_KEY,
            "Content-Type": "application/json",
        }

        data = {
            "model": "wan2.2-t2i-flash",
            "input": {
                "prompt": "风格为：像素风潦草卡通头像，8-bit形象，128x128低分辨率，故意不对称的像素块，手绘草稿转像素，未清理的粗糙边缘，FC游戏时代质感，充满个性。内容为"
                + desc
            },  # 您可以替换这里的提示词
            "parameters": {"size": "512*512", "n": 1},
        }
        url = f"{AIService.BASE_URL}/services/aigc/text2image/image-synthesis"

        # 发送 POST 请求
        response = HTTPRequests.post(
            url=url, json=data, headers=headers  # 使用 json 参数，会自动序列化为 JSON
        )
        data = json.loads(response)
        task_id = data.get("output", {}).get("task_id")
        return task_id

    def getImg(task_id):
        headers = {
            "X-DashScope-Async": "disable",
            "Authorization": "Bearer " + API_KEY,
            "Content-Type": "application/json",
        }
        url = f"{AIService.BASE_URL}/tasks/{task_id}"
        # 发送 POST 请求
        response = HTTPRequests.get(
            url=url, headers=headers  # 使用 json 参数，会自动序列化为 JSON
        )
        data = json.loads(response)
        url = data.get("output", {}).get("results")[0].get("url")
        return url
